﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NS
{
    //[HotUpdate]
    //[Description("单据编号+行数")]
    //public class FColFXBH : AbstractBillPlugIn
    //{
    //    public override void BarItemClick(BarItemClickEventArgs e)
    //    {
    //        base.BarItemClick(e);
    //        if (e.BarItemKey.Equals("tbSplitSave")||e.BarItemKey.Equals("tbSave"))
    //        {
    //            string strBillNo = Convert.ToString(this.View.Model.GetValue("FBillNo"));//单据编号
    //            int EntryRowCount = this.View.Model.GetEntryRowCount("FPOOrderEntry");
    //            for (int i = 0; i < EntryRowCount; i++)
    //            {
    //                this.View.Model.SetValue("FColFXBH", strBillNo + "-" + (i + 1), i);//分项编号
    //                this.View.UpdateView("FColFXBH");
    //            }
    //            //}
    //        }
    //    }
    //}
    [HotUpdate]
    [Description("单据编号+行数")]
    public class FColFXBH : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FBillNo");
            e.FieldKeys.Add("FPOOrderEntry");
            base.OnPreparePropertys(e);
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            string FBillNo = "";
            foreach (DynamicObject item in e.DataEntitys)
            {
                if (item["BillNo"] != null && Convert.ToString(item["BillNo"]) != "")
                {
                    FBillNo = Convert.ToString(item["BillNo"]);
                }
                string SQL = "/*dialect*/" + string.Format(@"update t set t.FColFXBH='{0}'+'-'+convert(varchar,t.fseq) from t_PUR_POOrderEntry t
                left join t_PUR_POOrder t1 on t.fid=t1.fid
                where t1.fbillno='{0}'", FBillNo);
                DBUtils.ExecuteDynamicObject(this.Context, SQL);
            }
        }
    }
}
